class DataCloneError extends Error {
    constructor(message) {
        super(message);
        this.name = this.constructor.name;
        // 可以添加更多的自定义属性
    }
}

window.postMessage = newFunc('postMessage', 5, function postMessage(type, eve) {
    // if (debug)
    let obj = arguments[0];
    if (obj instanceof PluginArray) {
        throw new DOMException('Failed to execute \'postMessage\' on \'Window\': PluginArray object could not be cloned.')
    }
    print('postMessage.call', arguments)
    debugger;
    if (type === 'message') {
        let listeners = mm.memory.allListeners.get('message');
        if (listeners !== undefined) {
            for (let i = 0; i < listeners.length; i++) {
                listeners[i](eve)
            }


        }

    }
    // console.log(mm.memory.listeners,)
})


MessagePort = newFunc('MessagePort', 1, function MessagePort(t) {
    // console.log('new MessagePort', t)
    mm.memory.private_data.set(this, {
        onmessage: null,
        onmessageerror: null,
        port: t
    })
})





; delete MessagePort.prototype.constructor;
;
Object.defineProperties(MessagePort.prototype, {
    [Symbol.toStringTag]:{
        value:'MessagePort'
    },

'onmessage': {

                  get: newFunc('onmessage', 10, function (){


                    if (!MessagePort.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;

            let res =  mm.memory.private_data.get(this)['onmessage'];
            if (debug)print('MessagePort.onmessage.get', res)

            return res
        }, {get: true}), enumerable:true, configurable:true,


             set: newFunc('onmessage', 1, function (value){
                if (debug)print('MessagePort.onmessage.set', value)
            mm.memory.private_data.get(this)['onmessage'] = value

            }, {set:true}),  enumerable:true, configurable:true,



        },
'onmessageerror': {

                  get: newFunc('onmessageerror', 10, function (){


                    if (!MessagePort.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;

            let res =  mm.memory.private_data.get(this)['onmessageerror'];
            if (debug)print('MessagePort.onmessageerror.get', res)

            return res
        }, {get: true}), enumerable:true, configurable:true,


             set: newFunc('onmessageerror', 1, function (value){
                if (debug)print('MessagePort.onmessageerror.set', value)
            mm.memory.private_data.get(this)['onmessageerror'] = value

            }, {set:true}),  enumerable:true, configurable:true,



        },
'close': {

            value: newFunc('close', 10, function () {

                      if (!MessagePort.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;
                    if (debug)print('MessagePort.close.call', arguments)

            }), writable:true, enumerable:true, configurable:true,



        },
'postMessage': {

            value: newFunc('postMessage', 10, function () {

                      if (!MessagePort.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;
                    if (debug)print('MessagePort.postMessage.call', arguments)

            }), writable:true, enumerable:true, configurable:true,



        },
'start': {

            value: newFunc('start', 10, function () {

                      if (!MessagePort.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;
                    if (debug)print('MessagePort.start.call', arguments)

            }), writable:true, enumerable:true, configurable:true,



        },
'constructor': {

            value: newFunc('constructor', 10, function () {

                      if (!MessagePort.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;
                    if (debug)print('MessagePort.constructor.call', arguments)

            }), writable:true, enumerable:false, configurable:true,



        },
})

Object.setPrototypeOf(MessagePort, EventTarget)
Object.setPrototypeOf(MessagePort.prototype, EventTarget.prototype)
MessagePort.__proto__ = EventTarget.prototype


MessageChannel = newFunc('MessageChannel', 5, function MessageChannel() {
    if (debug) print('MessageChannel.call', arguments)
    mm.memory.private_data.set(this, {
        port1: new MessagePort(1),
        port2: new MessagePort(2),
    })
})


Object.defineProperties(MessageChannel.prototype, {
    port1: {

        get: newFunc('port1', 10, function port1() {
            let res = mm.memory.private_data.get(this).port1;
            if (debug) print('MessageChannel.port1.get', res)

            return res
        }, {get: true}), enumerable: true, configurable: true,

    },
    port2: {

        get: newFunc('port2', 10, function port2() {
            let res = mm.memory.private_data.get(this).port2;
            if (debug) print('MessageChannel.port2.get', res)

            return res
        }, {get: true}), enumerable: true, configurable: true,

    },
    constructor:{value:()=>{}}

})


MessageEvent = newFunc('MessageEvent', 1, function MessageEvent(data) {
    if (debug) print('MessageEvent.call', arguments)
    mm.memory.private_data.set(this, {
        data: data,
        origin: null,
        lastEventId: null,
        source: null,
        ports: null,
        userActivation: null,
    })
})





; delete MessageEvent.prototype.constructor;
;
Object.defineProperties(MessageEvent.prototype, {
    [Symbol.toStringTag]:{
        value:'MessageEvent'
    },

'data': {

                  get: newFunc('data', 10, function (){


                    if (!MessageEvent.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;

            let res =  mm.memory.private_data.get(this)['data'];
            if (debug)print('MessageEvent.data.get', res)

            return res
        }, {get: true}), enumerable:true, configurable:true,

        },
'origin': {

                  get: newFunc('origin', 10, function (){


                    if (!MessageEvent.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;

            let res =  mm.memory.private_data.get(this)['origin'];
            if (debug)print('MessageEvent.origin.get', res)

            return res
        }, {get: true}), enumerable:true, configurable:true,

        },
'lastEventId': {

                  get: newFunc('lastEventId', 10, function (){


                    if (!MessageEvent.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;

            let res =  mm.memory.private_data.get(this)['lastEventId'];
            if (debug)print('MessageEvent.lastEventId.get', res)

            return res
        }, {get: true}), enumerable:true, configurable:true,

        },
'source': {

                  get: newFunc('source', 10, function (){


                    if (!MessageEvent.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;

            let res =  mm.memory.private_data.get(this)['source'];
            if (debug)print('MessageEvent.source.get', res)

            return res
        }, {get: true}), enumerable:true, configurable:true,

        },
'ports': {

                  get: newFunc('ports', 10, function (){


                    if (!MessageEvent.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;

            let res =  mm.memory.private_data.get(this)['ports'];
            if (debug)print('MessageEvent.ports.get', res)

            return res
        }, {get: true}), enumerable:true, configurable:true,

        },
'userActivation': {

                  get: newFunc('userActivation', 10, function (){


                    if (!MessageEvent.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;

            let res =  mm.memory.private_data.get(this)['userActivation'];
            if (debug)print('MessageEvent.userActivation.get', res)

            return res
        }, {get: true}), enumerable:true, configurable:true,

        },
'initMessageEvent': {

            value: newFunc('initMessageEvent', 10, function () {

                      if (!MessageEvent.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;
                    if (debug)print('MessageEvent.initMessageEvent.call', arguments)

            }), writable:true, enumerable:true, configurable:true,



        },
'constructor': {

            value: newFunc('constructor', 10, function () {

                      if (!MessageEvent.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;
                    if (debug)print('MessageEvent.constructor.call', arguments)

            }), writable:true, enumerable:false, configurable:true,



        },
})
Object.setPrototypeOf(MessageEvent, Event)
Object.setPrototypeOf(MessageEvent.prototype, Event.prototype)
MessageEvent.__proto__ = Event.prototype